<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('menu.inc');

function SaveNewCategory() {
	$kc_num = $_REQUEST['kc_num'];
	$kc_category = $_REQUEST['kc_category'];

	if (!isset($kc_num) || $kc_num <= 0) {
	   Error("SaveEditCategory: Interner Fehler: Keine ID-Nummer!");
	   return false;
	}

	if (!isset($kc_category) || strlen($kc_category) == 0) {
	   Error("Es muss ein Text eingegeben werden! Leere Eingabezeile wurde nicht gespeichert.");
	   return false;
	}

	$db = OpenDB();
	$query = "select kc_category from key_category where kc_num = $kc_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows == 1) {
	   Error("Schl&uuml;ssel $kc_num ist bereits vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "insert into key_category (kc_num, kc_category) values ($kc_num, '$kc_category')";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

function SaveEditCategory() {
	$kc_num = $_REQUEST['kc_num'];
	$kc_category = $_REQUEST['kc_category'];

	if (!isset($kc_num) || $kc_num <= 0) {
	   Error("SaveEditCategory: Interner Fehler: Keine ID-Nummer!");
	   return false;
	}

	if (!isset($kc_category) || strlen($kc_category) == 0) {
	   Error("Es muss ein Text eingegeben werden! Leere Eingabezeile wurde nicht gespeichert.");
	   return false;
	}

	$db = OpenDB();
	$query = "select kc_category from key_category where kc_num = $kc_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 1) {
	   Error("Schl&uuml;ssel $kc_num ist nicht vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "update key_category set kc_category = '$kc_category' where kc_num = $kc_num";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

$newcategory = $_REQUEST['newcategory'];
$editcategory = $_REQUEST['editcategory'];

if (!isset($editcategory)) {
   $editcategory = 0;
}

$savenewcategory = $_REQUEST['savenewcategory'];
$saveeditcategory = $_REQUEST['saveeditcategory'];

if (isset($savenewcategory)) {
   SaveNewCategory();
}

if (isset($saveeditcategory)) {
   SaveEditCategory();
}

?>
<form name="category" action="category.php" method="post">
<input type="hidden" name="headline" value="36">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<input type="hidden" name="newcategory" value="">
<table class="indent"><tr><td>
<?php
Button("Neue Kategorie", "category", "newcategory", "1");
echo "</td></tr></table>\n";
$db = OpenDB();
$query = "select kc_num, kc_category from key_category order by kc_num";
$result = QueryDB($db, $query);

if (!$result) {
   echo "</table></form><br>\n";
   require('footer.inc');
   exit;
}

$numrows = numrowsDB($result);

if ($numrows > 0 || isset($newcategory)) {
?>
<table class="sel">
   <tr>
      <th class="sel">&nbsp;</th>
      <th class="sel">Key</th>
      <th class="sel">Text</th>
   </tr>
<?php
   $row = 0;

   while ($row < $numrows) {
      $data = fetchDB($result, $row);
      $kc_num = $data[0];
      $kc_category = $data[1];

      if ($kc_num == $editcategory) {
         echo "<input type=\"hidden\" name=\"kc_num\" value=\"$kc_num\">\n";
	 echo "<input type=\"hidden\" name=\"saveeditcategory\" value=\"$kc_num\">\n";
	 echo "<tr><td class=\"selakt\">";
         ButtonImage("new.png", "category", "Neu");
         echo "</td><td class=\"sel\">$kc_num</td>";
         echo "<td class=\"sel\"><input type=\"text\" name=\"kc_category\" value=\"$kc_category\" size=40 maxlength=50></td></tr>\n";
      } else {
	 echo "<tr><td class=\"selakt\">";
         ButtonImageMove("edit.png", "category", "category.php", "editcategory=$kc_num&menu=$menu&headline=36");
         echo "</td><td class=\"sel\">$kc_num</td><td class=\"sel\">$kc_category</td></tr>";
      }

      $row++;
   }

   if (isset($newcategory)) {
      echo "<input type=\"hidden\" name=\"savenewcategory\" value=\"savenewcategory\">\n";
      echo "<tr><td class=\"selakt\">";
      ButtonImage("new.png", "category", "Speichern");
      echo "</td><td class=\"inputmust\"><input type=\"text\" name=\"kc_num\" size=7 maxlength=7></td>";
      echo "<td class=\"sel\"><input type=\"text\" name=\"kc_category\" size=40 maxlength=50></td></tr>\n";
   }

   echo "<table class=\"indent\"><tr><td>";
   Button("Neue Kategorie", "category", "newcategory", "1");
   echo "</td></tr></table>\n";
?>
</table>
</form>
<?php
}

require('footer.inc');
?>

